﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PlanProd.Contracts;
using WebAppPlanProduccion.Code;
using Web.Framework;
using WebAppPlanProduccion.Code.Security;

namespace WebAppPlanProduccion.App
{
    public partial class PedidoComercialEdit : BasePage
    {
        private PedidoComercial PedidoComercial
        {
            get;
            set;
        }

        public int PedidoComercialId
        {
            get
            {
                return (int)ViewState["PedidoComercialId"];
            }
            set
            {
                ViewState["PedidoComercialId"] = value;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            HasPermissionToSeeMe(PlanProdSecurity.PEDIDOCOMERCIAL_VER);

            if (!this.IsPostBack)
            {
                if (this.Request.QueryString["Id"] != null)
                {
                    PedidoComercialId = Convert.ToInt32(this.Request.QueryString["Id"]);
                    PedidoComercial = WebServiceHelper.GetPedidoComercialById(PedidoComercialId);
                    Bind();
                }
            }
        }

        protected override void ApplySecurity()
        {
            this.lnkActualizarProducto.Visible = HasPermissionFor(PlanProdSecurity.PEDIDOCOMERCIAL_DETALLE_IMPORTAR);

            if (this.GridPedidoComercialDetalle.DataSource != null)
            {
                this.GridPedidoComercialDetalle.Columns[0].Visible = HasPermissionFor(PlanProdSecurity.PEDIDOCOMERCIAL_DETALLE_ELIMINAR);
            }
        }

        private void Bind()
        {
            this.hidPedidoId.Value = PedidoComercial.Id.ToString();
            this.txtPedidoId.Text = PedidoComercial.Id.ToString();
            this.txtNombre.Text = PedidoComercial.Nombre;
            this.txtTemporada.Text = PedidoComercial.TemporadaNombre;
            this.txtDestino.Text = PedidoComercial.DestinoNombre;
            this.txtCantidadPrendas.Text = PedidoComercial.CantidadPrendas.ToString();

            this.GridPedidoComercialDetalle.DataSource = PedidoComercial.Detalle;
            this.GridPedidoComercialDetalle.DataBind();

            BindAuditory(PedidoComercial);
        }

        protected void GridPedidoComercialDetalle_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[0].Attributes.Add("title", "Eliminar");

                PedidoComercialDetalle pedidoComercialDetalle = (PedidoComercialDetalle)e.Row.DataItem;

                LinkButton btnEliminarCtrl = (LinkButton)e.Row.FindControl("btnEliminar");
                btnEliminarCtrl.OnClientClick = string.Format("return confirm('Está seguro que desea eliminar el siguiente registro? {0}')", pedidoComercialDetalle.ProductoIdExterno);
            }
        }

        protected void GridPedidoComercialDetalle_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            try
            {
                if (e.CommandName == "eliminar")
                {
                    int pedidoComercialId = Convert.ToInt32(this.GridPedidoComercialDetalle.DataKeys[Convert.ToInt32(e.CommandArgument)].Value);
                    WebServiceHelper.EliminarPedidoComercialDetalle(pedidoComercialId);

                    var pcd = PedidoComercial.Detalle.Where(d => d.Id == pedidoComercialId).Single();
                    PedidoComercial.Detalle.Remove(pcd);
                    Bind();
                }
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);
            }
        }

        protected void lnkCancelar_Click(object sender, EventArgs e)
        {
            try
            {
                Response.Redirect("/App/PedidoComercialList.aspx", true);
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);
            }
        }

        protected void lnkEliminarTodo_Click(object sender, EventArgs e)
        {
            try
            {
                WebServiceHelper.EliminarPedidoComercialDetalleAll(this.PedidoComercialId);
                PedidoComercial = WebServiceHelper.GetPedidoComercialById(this.PedidoComercialId);
                Bind();
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);
            }
        }
    }
}